package com.sgb.kjwl.viewmodel.self.userInfo;

import android.util.Base64;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.ui.activity.self.userInfo.ChangeMobileActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangeMobileModel extends BaseViewModel {
    private ChangeMobileActivity activity;
    private SelfRepertory repertory;

    public ChangeMobileModel(ChangeMobileActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void saveModel(String mobile, String code, String pwd) {
        Map<String, String> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("code", code);
        map.put("password", pwd);

        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().modifyPhone(mobile, android.util.Base64.encodeToString(pwd.getBytes(), Base64.DEFAULT), code).enqueue(new Callback<BaseEntity<LoginEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<LoginEntity>> call, Response<BaseEntity<LoginEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                MToast.showToast(activity, response.body().getMsg());
                if ("ok".equals(response.body().getState())) {
                    JPushInterface.deleteAlias(activity, 0);
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, response.body().getData().getPhone());
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, response.body().getData().getToken());
                    JPushInterface.setAlias(activity, 1, response.body().getData().getPhone());
                    EventBus.getDefault().postSticky(LoginEvent.make(EventConstant.EVENT_PHONE_EDIT, 0));
                    activity.finish();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<LoginEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

//        repertory.changeMobile(map)
//                .subscribe(new Consumer<BaseEntity>() {
//                    @Override
//                    public void accept(BaseEntity baseEntity) throws Exception {
//                        if ("ok".equals(baseEntity.getState())){
//                            MToast.showToast(activity, baseEntity.getData().toString());
//                            activity.finish();
//                        }else {
//                            MToast.showToast(activity, baseEntity.getData().toString());
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                    }
//                });
    }

    public void getCodeModel(String mobile) {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().modifyPhoneCode(mobile).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                MToast.showToast(activity, response.body().getMsg());
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

//        repertory.getRegisterCode(mobile)
//                .subscribe(new Consumer<BaseEntity>() {
//                    @Override
//                    public void accept(BaseEntity baseEntity) throws Exception {
//                        MToast.showToast(activity, baseEntity.getMsg());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                    }
//                });
    }
}
